package com.yaorun.freemarker;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yaorun.freemarker.bean.Animal;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkerTest1 {
	public static void main(String[] args) throws Exception {
		Animal animal = new Animal();
		animal.setName("小狗");
		animal.setPrice(88);
		Map map = new HashMap();
		// 得到类对象
		Class userCla = (Class) animal.getClass();
		map.put("title", userCla.getSimpleName());
		Map inputMap = new HashMap();
		/*
		 * 得到类中的所有属性集合
		 */
		Field[] fs = userCla.getDeclaredFields();
		List inputs = new ArrayList();
		map.put("inputs", Arrays.asList(fs));
		try {
			Configuration config = new Configuration();
			config.setDefaultEncoding("UTF-8");
			config.setDirectoryForTemplateLoading(
					new File(System.getProperty("user.dir") + "\\src\\main\\resources\\template"));

			Template template = config.getTemplate("user.ftl");
			template.process(map, new FileWriter(
					new File(System.getProperty("user.dir") + "\\src\\main\\resources\\template\\annimal.jsp")));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}	
